package com.sky.config;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.ParityFailureException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;


/**
 * 校验前端传过来的值是否为null
 */

@Slf4j
public class Calibration {

    /**
     * 套餐添加套餐菜品时，判断前端传过来的值是否为null
     *
     * @param setmealDTO
     */
    public static void addedSetmealVerification(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        Setmeal(setmeal);
        SetmealDish setmealDish = new SetmealDish();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            setmealDish.setCopies(dish.getCopies());
            setmealDish.setDishId(dish.getDishId());
            setmealDish.setName(dish.getName());
            setmealDish.setPrice(dish.getPrice());
            setmealDish.setSetmealId(dish.getSetmealId());
            setmealDish.setId(dish.getId());
        }
        SetmealDish(setmealDish);
    }

    /**
     * 判断Setmeal里面的值是否为null
     *
     * @param setmeal
     */
    public static void Setmeal(Setmeal setmeal) {
        log.info("套餐添加套餐菜品时，判断前端传过来的值是否为null,前端传过来的数据{}", setmeal);
        if (Objects.isNull(setmeal.getCategoryId())) {
            throw new ParityFailureException("分类id不能为空");
        }
        if (Objects.isNull(setmeal.getName())) {
            throw new ParityFailureException("套餐名称不能为空");
        }
        if (Objects.isNull(setmeal.getPrice())) {
            throw new ParityFailureException("套餐价格不能为空");
        }
        if (Objects.isNull(setmeal.getImage())) {
            throw new ParityFailureException("套餐图片不能为空");
        }
        if (Objects.isNull(setmeal.getStatus())) {
            throw new ParityFailureException("套餐状态不能为空");
        }
    }

    /**
     * 判断SetmealDish里面的值是否为null
     * @param setmealDish
     */
    public static void SetmealDish(SetmealDish setmealDish) {
        if (Objects.isNull(setmealDish.getDishId())) {
            throw new ParityFailureException("套餐里面的菜的菜品id不能为空");
        }
        if (Objects.isNull(setmealDish.getCopies())) {
            throw new ParityFailureException("套餐里面的菜的份数不能为空");
        }
        if (Objects.isNull(setmealDish.getName())) {
            throw new ParityFailureException("套餐里面的菜的名字不能为空");
        }
        if (Objects.isNull(setmealDish.getPrice())) {
            throw new ParityFailureException("套餐里面的菜的价格不能为空");
        }
    }

    /**
     * 套餐分页查询时，判断前端传过来的值是否为null
     */
    public static void pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询时，判断前端传过来的值是否为null,前端传过来的数据{}", setmealPageQueryDTO);
        if (Objects.isNull(setmealPageQueryDTO.getPage())) {
            throw new ParityFailureException("页码不能为空");
        }
        if (Objects.isNull(setmealPageQueryDTO.getPageSize())) {
            throw new ParityFailureException("每页数据条数不能为空");
        }
    }
}
